#include <debug.h>
#include <inttypes.h>
#include <round.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <hash.h>
#include "filesys/file.h"
#include "threads/thread.h"
#include "devices/block.h"

struct page
{
  struct hash_elem hash_elem; /* Hash table element. */
  struct thread *thread;
  void *addr;                 /* Virtual address. */
  int data_type; //0-file, 1-stack, 2-mmap
  
  struct file *file;
  unsigned offset;
  unsigned length;
 
  bool wirtable;
  block_sector_t sector; // 是否在内存中，在内存中为-1，修改见swap_out

  struct lock page_lock;
};

unsigned page_hash (const struct hash_elem *, void * UNUSED);
bool page_less (const struct hash_elem *, const struct hash_elem *, void * UNUSED);
struct page *page_lookup (struct hash *, const void *);
bool page_new_file (struct hash *, struct thread *thread, void *upage, bool, struct file *, unsigned offset, unsigned length);
void page_new_stack (struct hash *, struct thread *thread, void *);
bool page_new_mmap (struct hash *pt, struct thread *thread, void *vaddr, struct file *file, unsigned offset, unsigned length);
bool page_free (struct page *page);
void page_table_destroy (struct hash *pt);